<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="登陆编码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="success" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleCreate">
        添加
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" sortable="custom" align="center" width="80" />
      <el-table-column label="组织名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组织编码" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组织描述">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pageTotal>0" :total="pageTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="80px" style="width: 300px; margin-left:50px;">
        <el-form-item label="组织名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="组织描述" prop="email">
          <el-input v-model="temp.description" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getGroupList, creatGroup, updateGroup, deleteGroup } from '@/api/group'
import Pagination from '@/components/Pagination'
export default {
  name: 'GroupList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'info'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      pageTotal: 0, // 总条数
      listQuery: {
        page: 1, // 第几页
        size: 20, // 每页条数
        name: undefined
      },
      statusOptions: [1, 0],
      temp: {
        id: undefined,
        loginName: '',
        name: '',
        email: '',
        status: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑组织',
        create: '创建组织'
      },
      rules: {
        name: [
          { required: true, message: '请输入组织名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取数据列表
    getList() {
      this.listLoading = true
      const tempQuery = {
        page: this.listQuery.page - 1,
        size: this.listQuery.size,
        name: this.listQuery.name
      }
      getGroupList(tempQuery).then(res => {
        this.list = res.data.content
        this.pageTotal = res.data.totalElements // 获取总条数

        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    // 查询用户
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置表单
    resetTemp() {
      this.temp = {
        id: undefined,
        loginName: '',
        name: '',
        email: '',
        status: null
      }
    },
    // 创建用户
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加用户信息
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          creatGroup(this.temp).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$message({
              message: 'Created Successfully',
              type: 'success'
            })
          })
        }
      })
    },
    // 编辑用户信息
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 更新用户信息
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          console.log(tempData)
          updateGroup(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$message({
              message: 'Update Successfully',
              type: 'success'
            })
          })
        }
      })
    },
    // 删除用户信息
    handleDelete(row) {
      deleteGroup(row.id).then(() => {
        this.getList()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      })
    }
  }
}
</script>
<style lang="sass" scoped>
</style>
